package com.dream.inspect.modules.business.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.dream.inspect.common.utils.StringUtil;
import com.dream.inspect.modules.business.dao.AlwaysUseDao;
import com.dream.inspect.modules.business.entity.AlwaysUseEntity;
import com.dream.inspect.modules.business.service.AlwaysUseService;


@Service("alwaysUseService")
public class AlwaysUseServiceImpl extends ServiceImpl<AlwaysUseDao, AlwaysUseEntity> implements AlwaysUseService {

	@Autowired
	private AlwaysUseDao alwaysUseDao;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void insertEntity(Map<String, Object> params){
		
		String userId = params.get("userId").toString();
		// 添加之前先删除原有功能
		alwaysUseDao.delete(new EntityWrapper<AlwaysUseEntity>().eq("user_id", Long.parseLong(userId)));
		if(StringUtil.isNotEmpty(params.get("menuIds").toString())){
			String[] menuIds = params.get("menuIds").toString().split(",");
			// 添加常用功能
			for(String menuId : menuIds){
				AlwaysUseEntity entity = new AlwaysUseEntity();
				entity.setUserId(Long.parseLong(userId));
				entity.setMenuId(Long.parseLong(menuId));
				alwaysUseDao.insert(entity);
			}
		}
	}
	
}
